package example.demo_navi;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class Demo_navi extends Activity {

	private DrawerLayout dl;
	private ListView lv1;
	private ActionBarDrawerToggle abd;
	private String titi="";
	
	@SuppressLint("NewApi")
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_4);
        titi="Choa38";
        getActionBar().setTitle(titi);
        dl=(DrawerLayout) findViewById(R.id.drawer_layout);
        lv1=(ListView) findViewById(R.id.drawer_list);
        abd=new ActionBarDrawerToggle(this, dl, R.drawable.ic_drawer, R.string.open, R.string.close)
        {

			@Override
			public void onDrawerClosed(View drawerView) {
				// TODO Auto-generated method stub
				getActionBar().setTitle(titi);
				invalidateOptionsMenu();
			}

			@Override
			public void onDrawerOpened(View drawerView) {
				// TODO Auto-generated method stub
				getActionBar().setTitle("38quechoa");
				invalidateOptionsMenu();
			}
        	
        };
        
        dl.setDrawerListener(abd);
        ArrayAdapter<String> aad=new ArrayAdapter<String>(getBaseContext(), R.layout.item_list, getResources().getStringArray(R.array.demoli));
        lv1.setAdapter(aad);
        getActionBar().setHomeButtonEnabled(true);
        getActionBar().setDisplayHomeAsUpEnabled(true);
       lv1.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> par, View v, int pos,
					long id) {
				// TODO Auto-generated method stub
				String[] mi=getResources().getStringArray(R.array.demoli);
				titi=mi[pos];
				Profrag pr=new Profrag();
				Bundle bd=new Bundle();
				bd.putInt("position", pos);
				bd.putString("chara", getchare(pos));
				pr.setArguments(bd);
				
				FragmentManager fm=getFragmentManager();
				FragmentTransaction ft=fm.beginTransaction();
				ft.replace(R.id.content_frame, pr);
				ft.commit();
				dl.closeDrawer(lv1);
			}
        	
		});
       
    }

    protected String getchare(int pos){
    	switch (pos) {
		case 0:
			
			return "a";
		case 1:
			return "b";
		case 2:
			return "c";
		case 3:
			return "";

		default:
			return "a";
		}
    }
    
    @Override
	protected void onPostCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onPostCreate(savedInstanceState);
		abd.syncState();
	}


	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		boolean bo=dl.isDrawerOpen(lv1);
		menu.findItem(R.id.menu_settings).setVisible(bo);
		return super.onPrepareOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if (abd.onOptionsItemSelected(item)) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		// TODO Auto-generated method stub
		super.onConfigurationChanged(newConfig);
		abd.onConfigurationChanged(newConfig);
	}

	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_demo_navi, menu);
        return true;
    }
}
